package com.xysq.adapter;
/**
 * Copyright (c) 2015 XiaoYuanShenQi. All rights reserved.
 * <p/>
 * Author: Steward_Han
 * Date: 2015-7-10
 * Description: adapter of school's list
 */

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xysq.lemon.R;

import java.util.ArrayList;

/**
 * Created by Steward_Han on 2015/7/2.
 */
public class SchoolAdapter extends BaseAdapter {
    private ViewHolder viewHolder;
    private Context context = null;
    private ArrayList<String[]> schoolList;

    public SchoolAdapter(Context context, ArrayList<String[]> schoolList) {
        this.context = context;
        this.schoolList = schoolList;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (null != schoolList) {
            count = schoolList.size();
        }
        return count;
    }

    @Override
    public String[] getItem(int position) {
        String[] item = null;
        if (null != schoolList) {
            item = schoolList.get(position);
        }
        return item;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder = new ViewHolder();
        String[] schoolItem = getItem(position);
        if (null == convertView) {
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.school_item, null);

            viewHolder.txtSchoolName = (TextView) convertView.findViewById(R.id.txt_school_name);
            viewHolder.txtSchoolAddress = (TextView) convertView.findViewById(R.id.txt_school_address);
            viewHolder.imvPhone = (ImageView) convertView.findViewById(R.id.imv_school_phone);
            setListener(context, schoolItem[2]);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // set item values to the viewHolder:

        if (null != schoolItem) {
            viewHolder.txtSchoolName.setText(schoolItem[0]);
            viewHolder.txtSchoolAddress.setText(schoolItem[1]);
            viewHolder.phoneNumber = schoolItem[2];
        }
        return convertView;
    }

    private void setListener(final Context context, final String phoneNumber) {
        viewHolder.imvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);*/
                Uri smsToUri = Uri.parse("smsto:"+phoneNumber);
                Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
                context.startActivity(intent);
            }
        });
    }

    private static class ViewHolder {
        ImageView imvPhone;
        TextView txtSchoolName;
        TextView txtSchoolAddress;
        String phoneNumber;
    }
}
